package com.ruoyi.custom.service.impl;

import java.util.List;

import com.ruoyi.custom.domain.ConsultingTeacher;
import com.ruoyi.custom.mapper.ConsultingTeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.custom.mapper.PotentialClientsMapper;
import com.ruoyi.custom.domain.PotentialClients;
import com.ruoyi.custom.service.IPotentialClientsService;

/**
 * 潜在客户Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-25
 */
@Service
public class PotentialClientsServiceImpl implements IPotentialClientsService 
{
    @Autowired
    private PotentialClientsMapper potentialClientsMapper;
    @Autowired
    private ConsultingTeacherMapper consultingTeacherMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 查询潜在客户
     * 
     * @param id 潜在客户主键
     * @return 潜在客户
     */
    @Override
    public PotentialClients selectPotentialClientsById(Long id)
    {
        return potentialClientsMapper.selectPotentialClientsById(id);
    }

    /**
     * 查询潜在客户列表
     * 
     * @param potentialClients 潜在客户
     * @return 潜在客户
     */
    @Override
    public List<PotentialClients> selectPotentialClientsList(PotentialClients potentialClients)
    {
        List<PotentialClients> potentialClients1 = potentialClientsMapper.selectPotentialClientsList(potentialClients);
        Object o = redisTemplate.opsForValue().get("all-potentialClients");
        if(o==null){
            redisTemplate.opsForValue().set("all-potentialClients",potentialClients1);
        }

        return potentialClients1;
    }

    /**
     * 新增潜在客户
     * 
     * @param potentialClients 潜在客户
     * @return 结果
     */
    @Override
    public int insertPotentialClients(PotentialClients potentialClients)
    {
        return potentialClientsMapper.insertPotentialClients(potentialClients);
    }

    /**
     * 修改潜在客户
     * 
     * @param potentialClients 潜在客户
     * @return 结果
     */
    @Override
    public int updatePotentialClients(PotentialClients potentialClients)
    {
        return potentialClientsMapper.updatePotentialClients(potentialClients);
    }

    /**
     * 批量删除潜在客户
     * 
     * @param ids 需要删除的潜在客户主键
     * @return 结果
     */
    @Override
    public int deletePotentialClientsByIds(Long[] ids)
    {
        return potentialClientsMapper.deletePotentialClientsByIds(ids);
    }

    /**
     * 删除潜在客户信息
     * 
     * @param id 潜在客户主键
     * @return 结果
     */
    @Override
    public int deletePotentialClientsById(Long id)
    {
        return potentialClientsMapper.deletePotentialClientsById(id);
    }

    /**
     * 分配咨询
     */
    @Override
    public void addTeacherClients( Long[] ids) {
        // 查看是否有改潜在学员
        Long id= ids[0];
        PotentialClients potentialClients = potentialClientsMapper.selectPotentialClientsById(id);
        if(potentialClients!=null){
            List<ConsultingTeacher> all =(List<ConsultingTeacher>) redisTemplate.opsForValue().get("teacher_consultation");

            // 将第一个元素拿出
            ConsultingTeacher consultingTeacher = all.get(0);
            String name = consultingTeacher.getName();
            potentialClients.setConsultant(name);
            potentialClientsMapper.updatePotentialClients(potentialClients);
            // 将第一个元素移动到最后一个
            all.remove(0);
            all.add(consultingTeacher);
            redisTemplate.opsForValue().set("teacher_consultation",all);
        }


    }

    @Override
    public void regular(Long[] ids) {
        Long id= ids[0];
        PotentialClients potentialClients = potentialClientsMapper.selectPotentialClientsById(id);
        potentialClients.setCurrentState("正式学员");
        potentialClientsMapper.updatePotentialClients(potentialClients);
    }
}
